﻿/*
COPYRIGHT (C) 2009 EPISERVER AB

THIS FILE IS PART OF DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS.
DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.DynamicContent;
using EPiServer.Core;

namespace EPiServer.Research.DynamicContent
{
    public abstract class DynamicContentBase : IDynamicContent
    {
        protected PropertyDataCollection properties;

        public DynamicContentBase()
        {
        }

        #region IDynamicContent Members

        public abstract System.Web.UI.Control GetControl(PageBase hostPage);
        

        public abstract EPiServer.Core.PropertyDataCollection Properties{get;}

        public abstract string Render(PageBase hostPage);

        public abstract bool RendersWithControl{get;}


        static private string DecodeFrom64(string encodedData)
        {

            try
            {
                byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
                string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

                return returnValue;

            }
            catch (Exception e)
            {
                throw;
            }


        }

        static private string EncodeTo64(string toEncode)
        {

            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);

            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);

            return returnValue;

        }

        public string State
        {
            get
            {
                if (Properties == null) return string.Empty;
                StringBuilder sb = new StringBuilder();
                foreach (PropertyData pd in Properties)
                {
                    sb.Append(EncodeTo64(pd.ToString()) + "|"); //TODO: base 64 encode

                }

                return sb.ToString();
            }
            set
            {
                string[] parts = value.Split('|');
                for (int i = 0; i < Properties.Count; i++)
                {
                    if (parts.Length > i) Properties[i].ParseToSelf(DecodeFrom64(parts[i]));
                }
            }
        }

        #endregion
    }
}
